package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.ArticleLikeDao;
import com.jbp.exception.RRException;
import com.jbp.model.ArticleLike;
import com.jbp.model.User;
import com.jbp.service.ArticleLikeService;
import com.jbp.service.UserLevelService;
import com.jbp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import java.util.Map;

/**
 * 动态的点赞(ArticleLike)表服务实现类
 * @author cc
 * @since 2020-08-25 14:37:34
 */
@Service
public class ArticleLikeServiceImpl extends ServiceImpl<ArticleLikeDao, ArticleLike> implements ArticleLikeService {
    @Autowired
    private UserService userService;
    @Autowired
    private UserLevelService userLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ArticleLike> queryWrapper = new QueryWrapper<>();
        if (params.get("articleId") == null){
            throw new RRException("动态id不能为空");
        }
        queryWrapper.eq("article_id",params.get("articleId"));
        queryWrapper.eq("status",1);
        queryWrapper.orderByDesc("update_time");
        IPage<ArticleLike> page = this.page(
                new Query<ArticleLike>().getPage(params),
                queryWrapper
        );

        for (ArticleLike articleLike : page.getRecords()) {
            User reader = userService.getById(articleLike.getReaderId());
            if (reader!=null) {
                articleLike.setReaderName(reader.getName());
                articleLike.setReaderImg(reader.getImg());
                articleLike.setReaderSex(reader.getSex());
                articleLike.setReaderLevel(reader.getLevel());
                articleLike.setReaderLevelOn(userLevelService.levelOn(reader.getId()));
            }
        }
        return new PageUtils(page);
    }
}